package com.itheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.*;
import com.itheima.result.Result;
import com.itheima.ship.po.Employee;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.vo.PageResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.security.auth.login.AccountException;
import javax.security.auth.login.AccountLockedException;
import javax.security.auth.login.AccountNotFoundException;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 郑哥
 * @since 2024-05-18
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee getEmployeeByUsernameAndPassword(EmployeeLoginDTO employeeLoginDTO) throws AccountException {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //获取数据库已经注册的用户信息
        Employee employee = employeeMapper.getEmployeeByUsername(username);

        //账号不存在
        if (employee == null) {
            throw new AccountNotFoundException("账号不存在");
        }

        //使用md5对前端传输来的密码进行加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            throw new AccountNotFoundException("密码错误");
        }
        //判断用户是否被禁用 status==1 为启用  status == 2 为禁用
        if (employee.getStatus() == 0) {
            throw new AccountLockedException("账号被锁定");
        }

        return employee;
    }

    @Override
    public void registerEmployee(EmployeeRegisterDTO employeeRegisterDTO) throws AccountException {
        String username = employeeRegisterDTO.getUsername();
        String password = employeeRegisterDTO.getPassword();
        String repassword = employeeRegisterDTO.getRepassword();

        Employee employee = employeeMapper.getEmployeeByUsername(username);

        if (employeeRegisterDTO == null) {
            throw new AccountNotFoundException("前端输入信息为null");
        }
        if (!password.equals(repassword)) {
            throw new AccountException("两次输入的密码不一致");
        }

        if (employee == null) {
            password = DigestUtils.md5DigestAsHex(password.getBytes());
            Employee employee1 = employee.builder()
                    .name(employeeRegisterDTO.getName())
                    .username(username)
                    .password(password)
                    .phone(employeeRegisterDTO.getPhone())
                    .status(1)
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .build();
            employeeMapper.insert(employee1);
        } else {
            throw new AccountException("用户名已存在");
        }


        //对密码进行加密

    }

    @Override
    public Result<PageResultVO> selectEmployeeByPage(EmployeePageDTO employeePageDTO) {
        String name = employeePageDTO.getName();
        Page<Employee> page = Page.of(employeePageDTO.getPage(), employeePageDTO.getPageSize());
        page.addOrder(new OrderItem("update_time", false));
        LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<Employee>();
        LambdaQueryWrapper<Employee> like = employeeLambdaQueryWrapper.like(name != null, Employee::getName, name);
        Page<Employee> page1 = this.page(page, like);
        BeanUtil.copyToList(page1.getRecords(), Employee.class);

        return Result.success(new PageResultVO(page1.getTotal(), page1.getRecords()));



    }

    @Override
    public void insertEmployee(InsertEmployeeDTO insertEmployeeDTO) {
        Employee employee = Employee.builder()
                .name(insertEmployeeDTO.getName())
                .username(insertEmployeeDTO.getUsername())
                .password(DigestUtils.md5DigestAsHex("123456".getBytes()))
                .phone(insertEmployeeDTO.getPhone())
                .status(1)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();

        this.save(employee);


    }

    @Override
    public Employee selectEmployeeById(Long id) {
        Employee employee = this.getById(id);
        employee.setPassword("****");
        return employee;
    }

    @Override
    public void updateEmployee(UpdateEmployeeDTO updateEmployeeDTO) {
        Employee employee = BeanUtil.copyProperties(updateEmployeeDTO, Employee.class);
        employee.setUpdateTime(LocalDateTime.now());
                    this.updateById(employee);
    }

    @Override
    public void updateEmployeeStatus(Integer status, Long id) {
       employeeMapper.updataStatusById(status, id);
    }


}